package com.zhang.zhangapp.view;


import android.os.CountDownTimer;
import android.support.v4.app.Fragment;

import com.zhang.zhangapp.R;
import com.zhang.zhangapp.utils.NetUtils;
import com.zhang.zhangapp.utils.T;

import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * Created by 罗肖 on 2017/6/29.
 */
public class BaseFragment extends Fragment {

    private SweetAlertDialog sweetAlertDialog;
    int i = -1;

    public boolean showLoadingDialog() {
        if (!NetUtils.isConnected(getContext())) {
            T.showShort(getContext(), "网络异常，请检查网络连接");
            return false;
        }
        if (sweetAlertDialog != null && sweetAlertDialog.isShowing())
            return true;
        i = -1;
        sweetAlertDialog = new SweetAlertDialog(getContext(), SweetAlertDialog.PROGRESS_TYPE)
                .setTitleText("数据加载中...");
        sweetAlertDialog.show();
        sweetAlertDialog.setCancelable(false);
        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.success_stroke_color));
//        new CountDownTimer(800 * 7, 800) {
//            public void onTick(long millisUntilFinished) {
//                // you can change the progress bar color by ProgressHelper every 800 millis
//                i++;
//                switch (i) {
//                    case 0:
//                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.blue_btn_bg_color));
//                        break;
//                    case 1:
//                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_deep_teal_50));
//                        break;
//                    case 2:
//                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.success_stroke_color));
//                        break;
//                    case 3:
//                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_deep_teal_20));
//                        break;
//                    case 4:
//                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_blue_grey_80));
//                        break;
//                    case 5:
//                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.warning_stroke_color));
//                        break;
//                    case 6:
//                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.success_stroke_color));
//                        break;
//                }
//            }
//
//            public void onFinish() {
//                i = -1;
//            }
//        }.start();

        return true;
    }

    public boolean showLoadingDialog(String title) {
        if (!NetUtils.isConnected(getContext())) {
            T.showShort(getContext(), "网络异常，请检查网络连接");
            return false;
        }
        if (sweetAlertDialog != null && sweetAlertDialog.isShowing())
            return true;
        i = -1;
        sweetAlertDialog = new SweetAlertDialog(getContext(), SweetAlertDialog.PROGRESS_TYPE)
                .setTitleText(title);
        sweetAlertDialog.show();
        sweetAlertDialog.setCancelable(false);
        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.success_stroke_color));
//        new CountDownTimer(800 * 7, 800) {
//            public void onTick(long millisUntilFinished) {
//                // you can change the progress bar color by ProgressHelper every 800 millis
//                i++;
//                switch (i) {
//                    case 0:
//                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.blue_btn_bg_color));
//                        break;
//                    case 1:
//                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_deep_teal_50));
//                        break;
//                    case 2:
//                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.success_stroke_color));
//                        break;
//                    case 3:
//                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_deep_teal_20));
//                        break;
//                    case 4:
//                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_blue_grey_80));
//                        break;
//                    case 5:
//                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.warning_stroke_color));
//                        break;
//                    case 6:
//                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.success_stroke_color));
//                        break;
//                }
//            }
//
//            public void onFinish() {
//                i = -1;
//                sweetAlertDialog.setTitleText("Success!")
//                        .setConfirmText("OK")
//                        .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
//            }
//        }.start();

        return true;
    }

    public void dismisionDalog() {
        if (sweetAlertDialog != null) {
            sweetAlertDialog.dismissWithAnimation();
        }
    }

}